package pl.edu.pw.MidEmu.UI.Controllers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Random;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import pl.edu.pw.MidEmu.Base.Controllers.BaseController;
import pl.edu.pw.MidEmu.Common.Consts;
import pl.edu.pw.MidEmu.Common.CopyManager;
import pl.edu.pw.MidEmu.Common.Models.EthernetInterfaceModel;
import pl.edu.pw.MidEmu.Common.Models.MidasModel;
import pl.edu.pw.MidEmu.Logic.ConfigurationService;
import pl.edu.pw.MidEmu.Logic.ProcessHandler;
import pl.edu.pw.MidEmu.UI.Views.MidasLoaderView;
/**
 * Controller for loading new MIDAS platform
 * @author Mateusz Starodab
 *
 */
public class MidasLoaderController extends BaseController<MidasLoaderView> implements ActionListener
{
	private ConfigurationService _configurationService;

	public MidasLoaderController()
	{
		_configurationService = new ConfigurationService();
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		if (e.getSource() instanceof JButton)
		{
			JButton button = (JButton) e.getSource();
			if (button.getName() == Consts.MidasLoaderFrame_StartMidas)
			{
				if (getView().validateMIDAS())
				{
					String binariesDir = getView().getBinariesDir();
					Random r = new Random(System.currentTimeMillis());
					int uid = r.nextInt();
					if(uid == Integer.MIN_VALUE)
						uid = Integer.MIN_VALUE +1;
					String uidString = Integer.toString(Math.abs(uid));
					File newDir = new File("./temp/" + uidString);
					String newDirPath = "";
					try
					{
						newDirPath = newDir.getCanonicalPath();
					}
					catch (IOException e1)
					{
						e1.printStackTrace();
					}
					CopyManager.copyFiles(binariesDir, newDirPath);

					Boolean isCentralNode = getView().isCentralNode();
					EthernetInterfaceModel ethernetInterface = getView().getEthernetInterface();
					String nodeName = uidString;
					String libPath = getView().getLibPath();
					String className = getView().getClassName();
					String params = getView().getParams();

					String tempParams = params.trim();

					if (isCentralNode)
						_configurationService.setCentralNodeConfiguration(newDirPath, nodeName, ethernetInterface.getIpAddress());
					else
						_configurationService.setUserNodeConfiguration(newDirPath, nodeName, ethernetInterface.getIpAddress(), getView().getCentralNodeID(), getView().getCentralNodeIP());

					ProcessHandler handler = new ProcessHandler(className, tempParams, "", newDirPath, libPath, Consts.MidasLoaderFrame_Name);
					handler.start();

					MidasModel model = new MidasModel();
					model.setClassName(className);
					model.setBinariesDirectory(binariesDir);
					model.setWorkDirectory(newDirPath);
					model.setClassPath(libPath);
					model.setParameters(params);
					model.setNodeName(nodeName);
					model.setIsCentralNode(isCentralNode);
					model.setTemporaryDir(newDirPath);
					model.setIpAddress(ethernetInterface.getIpAddress());
					model.setInterfaceName(ethernetInterface.getDisplayName());
					ethernetInterface.setFree(false);

					model.save();

					getView().registerMidas(model, handler);

					getView().dispose();
				}
				else
					getView().reportValidationErrors();
			}
			else if (button.getName() == Consts.MidasLoaderFrame_CancelButton)
			{
				getView().dispose();
			}
			else if (button.getName() == Consts.MidasLoaderFrame_BinkDirButton)
			{
				getView().showPickupBinariesDirExecDialog();
			}
		}
	}

	public void fillModel(DefaultComboBoxModel model)
	{
		List<EthernetInterfaceModel> ethernetModels = getView().getFreeInterfaces();
		if (ethernetModels.size() == 0)
		{
			getView().reportNoFreeInterfaces();
			return;
		}

		for (EthernetInterfaceModel ethernetModel : ethernetModels)
		{
			model.addElement(ethernetModel);
		}
	}
}
